using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;

namespace GuavaLib
{
    public class ViewLabel : System.Windows.Forms.Label
    {
        string[] view;
        GSelect itemSource;

        public GSelect ItemSource
        {
            get { return itemSource; }
            set
            {
                itemSource = value;
                if (itemSource != null)
                {
                    itemSource.SelectedItemChanged += new GSelect.ComboBoxItemChanged(itemSource_SelectedItemChanged);
                    this.Text = "";
                }
            }
        }

        void itemSource_SelectedItemChanged(object sender, GuavaRowSelectedEventArgs args)
        {
            string[] temp = ParsedView (args.RowInfo.RowData);
            this.Text = String.Join(Environment.NewLine, temp);
        }

        public string[] View
        {
            get { return view; }
            set { view = value; }
        }
        
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get
            {
                if (this.DesignMode) return this.Name;
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        public string[] ParsedView (System.Data.DataRowView drv)
        {
            string[] temp = new string[view.Length];
            for (int i = 0; i < view.Length; i++) temp[i] = view[i];
            
            foreach (System.Data.DataColumn dc in drv.DataView.Table.Columns)
            {
                for (int i = 0; i < temp.Length; i++) temp[i] = temp[i].Replace(String.Concat("<", dc.ColumnName, ">"), drv[dc.ColumnName].ToString());
            }
            return temp;
        }
    }
}
